﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class InsertNode : BasePage
    {
        private Lc_NodeBLL lnb = new Lc_NodeBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProcess();
            }

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void BindProcess()
        {
            DataTable table = lpb.GetAllInfo("", "");
            if (table != null && table.Rows.Count > 0)
            {
                ddlProcess.DataSource = table;
                ddlProcess.DataTextField = "C_PROCESS_NAME";
                ddlProcess.DataValueField = "N_PROCESS_ID";
                ddlProcess.DataBind();
                ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            else
            {
                MessageBox.Show(Page, "请添加流程信息");
                btnSave.Enabled = false;
                return;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //判断录入资料的正确性
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show(this, "节点名不能为空");
                txtName.Focus();
                return;
            }
            if (ddlProcess.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点所属流程");
                ddlType.Focus();
                return;
            }
            if (ddlState.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点状态");
                ddlState.Focus();
                return;
            }
            if (ddlType.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点类型");
                ddlType.Focus();
                return;
            }

            //实例化表资料对象
            Lc_Node model = new Lc_Node();

            model.C_NODE_NAME = txtName.Text.Trim();

            model.N_DOIING_TIME = 10;

            model.N_NODE_STATE = decimal.Parse(ddlState.SelectedValue.ToString());

            model.N_TRANSACTION_TIME = 0;

            model.N_NODE_TYPE = decimal.Parse(ddlType.SelectedValue.ToString());

            model.N_NODE_OPERATOR_ID = decimal.Parse(lognUser.USER_ID.ToString());

            model.N_NODE_OPERATION_TIME = DateTime.Now;

            model.N_PROCESS_ID = decimal.Parse(ddlProcess.SelectedValue.ToString());

            int count = lnb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                MessageBox.Show(Page, "保存成功");
                ClearControl();
                return;
            }
            else
            {
                MessageBox.Show(Page, "保存失败");
                return;
            }
        }

        private void ClearControl()
        {
            txtName.Text = "";
            ddlState.SelectedValue = "-1";
            ddlType.SelectedValue = "-1";
            ddlProcess.SelectedValue = "-1";
        }
    }
}